package cn.nawang.ebeim.api.utils;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @ClassName: DateOperate
 * @Description: TODO
 * @author 张建峰
 * @date 2015年8月6日 下午5:31:35
 * @version v1.0
 */
public class DateOperate {

  public static final String DEFAULTSTYLE = "yyyy-MM-dd";

  public static final String DATE_TIME_STYLE_All = "yyyy-MM-dd HH:mm:ss";

  public static final String DATE_TIME_STYLE_TIME = "HH:mm:ss";


  public static String getNowYear() {
    Calendar cd = Calendar.getInstance();
    int year = cd.get(Calendar.YEAR);
    return Integer.toString(year);
  }

  public static String getNowMonth() {
    Calendar cd = Calendar.getInstance();
    int month = cd.get(Calendar.MONTH) + 1;
    if (month < 10)
      return "0" + Integer.toString(month);
    else
      return Integer.toString(month);
  }

  public static String getNowDay() {
    Calendar cd = Calendar.getInstance();
    int day = cd.get(Calendar.DAY_OF_MONTH);
    if (day < 10)
      return "0" + Integer.toString(day);
    else
      return Integer.toString(day);
  }

  public static String getNowHour() {
    Calendar cd = Calendar.getInstance();
    int hour = cd.get(Calendar.HOUR_OF_DAY);
    return Integer.toString(hour);
  }

  public static String getNowMinute() {
    Calendar cd = Calendar.getInstance();
    int minute = cd.get(Calendar.MINUTE);
    return Integer.toString(minute);
  }

  public static String getNowSecond() {
    Calendar cd = Calendar.getInstance();
    int second = cd.get(Calendar.SECOND);
    return Integer.toString(second);
  }

  public static Date formatStringToDate(String dateString, String formatStyle) {
    Date date = null;
    if (formatStyle == null || formatStyle.equals("") == true) formatStyle = DEFAULTSTYLE;

    try {
      if (dateString == null || dateString.trim().equals("")) return null;
      return new SimpleDateFormat(formatStyle).parse(dateString);
    } catch (ParseException e) {
      date = null;
      e.printStackTrace();
    }
    return date;
  }

  public static String getNowDate(String formatStyle) {
    if (formatStyle == null || formatStyle.equals("")) formatStyle = DEFAULTSTYLE;
    Date now = new Date();
    SimpleDateFormat formater = new SimpleDateFormat(formatStyle);
    return formater.format(now);
  }

  public static boolean compareDate(Date date1, Date date2) {
    boolean flag = false;
    try {
      if (date1.after(date2) == true)
        flag = true;
      else
        flag = false;
    } catch (Exception ex) {
      flag = false;
      ex.printStackTrace();
    }
    return flag;
  }

  public static int getWeekendDays(String beginDate, String endDate) {
    int count = 0;
    Date begin = formatStringToDate(beginDate, DEFAULTSTYLE);
    Date end = formatStringToDate(endDate, DEFAULTSTYLE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(begin);
    while (true) {
      Date tempDate = calendar.getTime();
      if (tempDate.after(end))
        break;
      else {
        if ((calendar.get(Calendar.DAY_OF_WEEK) == 7) || (calendar.get(Calendar.DAY_OF_WEEK) == 1))
          count++;
        calendar.add(Calendar.DATE, 1);
      }
    }
    return count;
  }


  public static Calendar convertDateStringToCalendar(String dateString) {
    Date date = formatStringToDate(dateString, DEFAULTSTYLE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
  }

  public static Calendar convertDateStringToCalendar(String dateString, String timeStyle) {
    if (null == timeStyle || "".equals(timeStyle.trim())) {
      timeStyle = DEFAULTSTYLE;
    }
    Date date = formatStringToDate(dateString, timeStyle);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
  }

  public static String getFirstDayOfWeek(String dateString) {
    int nowweek = DateOperate.getDayOfWeek(dateString);
    int distance = 1 - nowweek;
    String firstDayOfWeek = DateOperate.getAnyDate(dateString, distance);
    return firstDayOfWeek;
  }


  public static String getLastDayOfWeek(String dateString) {
    int nowweek = DateOperate.getDayOfWeek(dateString);
    int distance = 7 - nowweek;
    String lastDayOfWeek = DateOperate.getAnyDate(dateString, distance);
    return lastDayOfWeek;
  }

  public static String getLastDayOfMonth(String year, String month) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, Integer.parseInt(year));
    c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
    int day = c.getActualMaximum(Calendar.DAY_OF_MONTH);
    return Integer.toString(day);
  }


  public static int getDayOfWeek(String dateString) {
    Calendar calendar = convertDateStringToCalendar(dateString);
    int week = calendar.get(Calendar.DAY_OF_WEEK);
    if (week == 1)
      week = 7;
    else
      --week;
    return week;
  }

  public static String getAnyDate(String nowDate, int days) {
    Calendar calendar = convertDateStringToCalendar(nowDate);
    calendar.add(Calendar.DATE, days);
    Date nextDate = calendar.getTime();
    return formatDateToString(nextDate, DEFAULTSTYLE);

  }

  public static String formatDateToString(Date date, String formatStyle) {
    if (formatStyle == null || formatStyle.equals("") == true) formatStyle = DEFAULTSTYLE;
    SimpleDateFormat formatter = new SimpleDateFormat(formatStyle);
    return formatter.format(date);
  }

  public static long getApartDate(String startDate, String endDate) {
    long result = 0;
    try {
      Date begin = formatStringToDate(startDate, DEFAULTSTYLE);
      Date end = formatStringToDate(endDate, DEFAULTSTYLE);
      long l = end.getTime() - begin.getTime();
      result = l / (24 * 60 * 60 * 1000);

    } catch (Exception ex) {
      result = -1;
      ex.printStackTrace();
    }
    return result;
  }

  public static String getFrontMonth(int month) {
    int frontMonth = 0;

    if (month > 12 || month < 1)
      frontMonth = -1;
    else {
      if (month == 1)
        frontMonth = 12;
      else
        frontMonth = --month;
    }
    if (frontMonth < 10)
      return "0" + Integer.toString(frontMonth);
    else
      return Integer.toString(frontMonth);
  }


  public static Time strToTime(String strTime) {
    Date date = formatStringToDate(strTime, DATE_TIME_STYLE_TIME);
    java.sql.Time time = new java.sql.Time(date.getTime());
    return time;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 当天制定时间的时间戳
   * @param @param hour
   * @param @param minute
   * @param @param second
   * @param @return
   * @return long
   * @date 2015年10月26日上午9:04:57
   *
   * @修改人
   * @修改日期
   */
  public static long getTime(int hour, int minute, int second) {
    Calendar cl = new GregorianCalendar();
    cl.set(Calendar.HOUR_OF_DAY, hour);
    cl.set(Calendar.MINUTE, minute);
    cl.set(Calendar.SECOND, second);
    return cl.getTimeInMillis();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 计算两个时间戳天差
   * @param @param
   * @param @return
   * @return long
   * @date 2015年10月24日下午2:39:41
   *
   * @修改人
   * @修改日期
   */
  public static long getLateTime(long endTime) {
    Calendar ca = Calendar.getInstance();
    long today = ca.getTimeInMillis();
    long days = (endTime - today) / (1000 * 3600 * 24);
    return days;
  }

}
